<template>
  <div>
    <nav-header></nav-header>
    <nav-bread>
      <span slot="bread">订单列表</span>
    </nav-bread>
    <svg style="position: absolute; width: 0; height: 0; overflow: hidden;" version="1.1"
         xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
      <defs>
        <symbol id="icon-add" viewBox="0 0 32 32">
          <title>添加</title>
          <path class="path1"
                d="M15 17h-13.664c-0.554 0-1.002-0.446-1.002-1 0-0.552 0.452-1 1.002-1h13.664v-13.664c0-0.554 0.446-1.002 1-1.002 0.552 0 1 0.452 1 1.002v13.664h13.664c0.554 0 1.002 0.446 1.002 1 0 0.552-0.452 1-1.002 1h-13.664v13.664c0 0.554-0.446 1.002-1 1.002-0.552 0-1-0.452-1-1.002v-13.664z"></path>
        </symbol>
        <symbol id="icon-ok" viewBox="0 0 32 32">
          <title>确认</title>
          <path class="path1"
                d="M31.020 0.438c-0.512-0.363-1.135-0.507-1.757-0.406s-1.166 0.435-1.529 0.937l-17.965 24.679-5.753-5.67c-0.445-0.438-1.035-0.679-1.664-0.679s-1.219 0.241-1.664 0.679c-0.917 0.904-0.917 2.375 0 3.279l7.712 7.6c0.438 0.432 1.045 0.681 1.665 0.681l0.195-0.008c0.688-0.057 1.314-0.406 1.717-0.959l19.582-26.9c0.754-1.038 0.512-2.488-0.538-3.233z"></path>
        </symbol>
        <symbol id="icon-edit" viewBox="0 0 32 32">
          <title>编辑</title>
          <path class="path1"
                d="M28.287 8.51l-4.805-4.806 0.831-0.831c0.472-0.472 1.086-0.777 1.564-0.777 0.248 0 0.452 0.082 0.622 0.253l3.143 3.144c0.539 0.54 0.133 1.529-0.524 2.186l-0.831 0.831zM26.805 9.992l-1.138 1.138-4.805-4.806 1.138-1.138 4.805 4.806zM24.186 12.612l-14.758 14.762-4.805-4.806 14.758-14.762 4.805 4.806zM7.379 28.288l-4.892 1.224 1.223-4.894 3.669 3.67zM31.123 4.011l-3.143-3.144c-0.567-0.567-1.294-0.867-2.103-0.867-1.036 0-2.174 0.52-3.045 1.391l-20.429 20.436c-0.135 0.134-0.23 0.302-0.276 0.487l-2.095 8.385c-0.089 0.355 0.017 0.736 0.276 0.995 0.198 0.198 0.461 0.307 0.741 0.307 0.085 0 0.171-0.010 0.254-0.031l8.381-2.096c0.185-0.047 0.354-0.142 0.487-0.276l20.43-20.436c1.409-1.41 2.042-3.632 0.524-5.15v0z"></path>
        </symbol>
        <symbol id="icon-del" viewBox="0 0 32 32">
          <title>删除</title>
          <path class="path1"
                d="M11.355 4.129v-2.065h9.29v2.065h-9.29zM6.194 29.935v-23.742h19.613v23.742h-19.613zM30.968 4.129h-8.258v-3.097c0-0.569-0.463-1.032-1.032-1.032h-11.355c-0.569 0-1.032 0.463-1.032 1.032v3.097h-8.258c-0.569 0-1.032 0.463-1.032 1.032s0.463 1.032 1.032 1.032h3.097v24.774c0 0.569 0.463 1.032 1.032 1.032h21.677c0.569 0 1.032-0.463 1.032-1.032v-24.774h3.097c0.569 0 1.032-0.463 1.032-1.032s-0.463-1.032-1.032-1.032v0z"></path>
          <path class="path2"
                d="M10.323 9.806c-0.569 0-1.032 0.463-1.032 1.032v14.452c0 0.569 0.463 1.032 1.032 1.032s1.032-0.463 1.032-1.032v-14.452c0-0.569-0.463-1.032-1.032-1.032z"></path>
          <path class="path3"
                d="M16 9.806c-0.569 0-1.032 0.463-1.032 1.032v14.452c0 0.569 0.463 1.032 1.032 1.032s1.032-0.463 1.032-1.032v-14.452c0-0.569-0.463-1.032-1.032-1.032z"></path>
          <path class="path4"
                d="M21.677 9.806c-0.569 0-1.032 0.463-1.032 1.032v14.452c0 0.569 0.463 1.032 1.032 1.032s1.032-0.463 1.032-1.032v-14.452c0-0.569-0.463-1.032-1.032-1.032z"></path>
        </symbol>
        <symbol id="icon-clock" viewBox="0 0 32 32">
          <title>clock</title>
          <path class="path1" fill="#605f5f"
                d="M16 29c-7.168 0-13-5.831-13-13s5.832-13 13-13c7.168 0 13 5.832 13 13s-5.832 13-13 13zM16 0c-8.822 0-16 7.178-16 16s7.178 16 16 16c8.822 0 16-7.178 16-16s-7.178-16-16-16z"></path>
          <path class="path2" fill="#605f5f"
                d="M23.958 21.837l-6.958-6.489v-6.282c0-0.827-0.673-1.5-1.5-1.5s-1.5 0.673-1.5 1.5v6.934c0 0.414 0.174 0.814 0.477 1.098l7.435 6.934c0.279 0.259 0.642 0.402 1.023 0.402 0.415 0 0.814-0.174 1.096-0.477 0.564-0.605 0.532-1.555-0.073-2.12z"></path>
        </symbol>
      </defs>
    </svg>

    <div class="sales-list">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span style="font: 18px large">我的订单</span>
        </div>
        <div class="text item">
          <el-form size="small" :model="searchForm" inline class="demo-ruleForm">
            <el-col :span="24">
              <el-form-item label="　时间">
                <el-date-picker type="date" placeholder="开始时间" v-model="searchForm.startTime" style="width: 130px"></el-date-picker>
                <big>-</big>
                <el-date-picker type="date" placeholder="结束时间" v-model="searchForm.endTime" style="width: 130px"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="关键字">
                <el-input
                  type="text"
                  v-model="searchForm.keyWord"
                  autocomplete="off"
                  placeholder="水果名称"
                  style="width: 300px"
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="searchOrders">查询</el-button>
              </el-form-item>
            </el-col>
          </el-form>

          <el-table
            ref="multipleTable"
            :data="tableData"
            tooltip-effect="dark"
            style="width: 100%"
            stripe
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection"></el-table-column>
            <el-table-column prop="id" label="id"></el-table-column>
            <el-table-column prop="numbering" label="订单号"></el-table-column>
            <el-table-column prop="fruit" label="订单详情">
              <template slot-scope="scope"><img class="fImage" :src="('http://localhost:8080/file/image/'+scope.row.fImage)" alt="">
                <br>{{scope.row.fruit}}</template>
            </el-table-column>
            <el-table-column prop="amount" label="支付金额"></el-table-column>
            <el-table-column prop="discount" label="优惠金额"></el-table-column>
            <el-table-column label="订单状态">
              <template slot-scope="scope">
                {{statusArray[scope.row.status]}}
                <button style="color: red" v-if="scope.row.status === 0" @click="evaluate(scope.row.fruit , scope.row.id)">评价得积分</button>
              </template>
            </el-table-column>
            <el-table-column prop="salesTime" label="下单时间"></el-table-column>
          </el-table>

          <!-- 分页 -->
          <el-pagination
            style="margin-top: 20px;text-align: right"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[1, 3, 5, 10]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          ></el-pagination>
        </div>
      </el-card>
    </div>

    <evaluate-modal v-if="evaluateShow" v-on:close="closeModal">
      <div slot="modal-header">
        <h1>{{fruitName}}</h1>
        <button class="md-close" @click="evaluateShow=false" style="position: relative;font-size: 30px;transform: translate3d(1750%,-120%,0)"><i class="el-icon-circle-close"></i></button>
      </div>

      <div slot="modal-body">
        <el-rate
          v-model="rate"
          show-text
          style="position: relative;font-size: 70px;transform: translate3d(35%, -120%, 0px);">
        </el-rate>
        <textarea rows="10" cols="50" style="position: relative;transform: translate3d(30%,10%,0)" v-model="appraise" placeholder="请输入您的评价"></textarea>
      </div>

      <div slot="modal-footer">
        <el-button style="position: relative;transform: translate3d(-350%,30%,0)" type="primary" @click="submitAppraise">提交</el-button>
      </div>
    </evaluate-modal>

    <nav-footer></nav-footer>
  </div>
</template>

<script>
import NavHeader from "./../components/NavHeader";
import NavFooter from "./../components/NavFooter";
import NavBread from "./../components/NavBread";
import Modal from "./../components/Modal";
import axios from "axios";
import EvaluateModal from "../components/evaluateModal.vue";
export default {
  data() {
    return {
      evaluateShow: false,
      currentPage: 1,
      total: 0,
      pageSize: 3,
      statusArray:["已支付", "已评价", "已退款"],
      searchForm: {
        startTime: "",
        endTime:"",
        keyWord: ""
      },
      tableData: [],
      fruitName:"",
      orderId:"",
      rate:5,
      appraise:""
    };
  },
  components: {
    EvaluateModal,
    NavHeader,
    NavFooter,
    NavBread,
    Modal
  },
  //生命周期钩子函数 自动触发
  created() {
    this.searchOrders();
  },
  computed: {
    test() {
      return "123";
    }
  },
  methods: {
    closeModal(){
      this.evaluateShow = false;
    },
    //提交评价
    submitAppraise() {
      let params = {
        orderId: this.orderId,
        rate: this.rate,
        appraise: this.appraise
      };
      axios.post("/order/submitAppraise",params,{ headers:{content:"application/json;charset=UTF-8" }})
        .then(response =>{
          //接收后端返回的数据总条数 total 和 对应页码的数据 data
          let res = response.data;
          // 如果当前页没有数据 且 排除第一页
          if (res.code === 0) {
            this.$message.success("评价成功,获得"+res.addPoints+"积分");
            JSON.parse(sessionStorage.getItem("user")).id =res.totalPoints;
            this.evaluateShow = false;
            this.appraise = null;
            this.rate = 5;
            this.searchOrders();
          }else {
            this.$message.error("提交失败");
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    evaluate(fruitName,orderId){
      this.fruitName = fruitName;
      this.orderId = orderId;
      this.evaluateShow = true;
    },
    searchOrders(){
      console.log("初始化列表")
      let params = {
        pageSize: this.pageSize,
        currentPage: this.currentPage,
        startTime:this.searchForm.startTime,
        endTime:this.searchForm.endTime,
        keyWord:this.searchForm.keyWord,
        cid:JSON.parse(sessionStorage.getItem("user")).id
      };
      console.log(params);
      axios.post("/order/customerSearchOrders",params,{ headers:{content:"application/json;charset=UTF-8" }})
        .then(response =>{
          //接收后端返回的数据总条数 total 和 对应页码的数据 data
          let { total, data } = response.data;
          this.total = total;
          this.tableData = data;
          console.log(data);
          // 如果当前页没有数据 且 排除第一页
          if (!data.length && this.currentPage !== 1) {
            //页码减去1
            this.currentPage -= 1;
            //再调自己
            this.searchOrders();
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleEdit() {},
    handleDelete() {},
    //每页显示条数改变触发的函数
    handleSizeChange(val) {
      //保存每页显示的条数
      this.pageSize = val;
      //调用分页函数
      this.searchOrders();
    },
    //当前页码改变触发的函数
    handleCurrentChange(val) {
      //保存当前页码
      this.currentPage = val;
      //调用分页函数
      this.searchOrders();
    }
  },
  //过滤器
  filters: {
    //过滤时间的函数
    filterCtime(ctime) {
      return moment(ctime).format("YYYY-MM-DD HH:mm:ss");
    }
  }
};
</script>

<style>
.fImage{
  width: 70px;
  height: 70px
}
.sales-list {
  .el-card {
    .el-card__header {
      font-size: 20px;
      font-weight: 600;
      background-color: #f1f1f1;
    }
    .el-card__body {
      .el-pagination{
        margin-top: 20px;
      }
    }
  }
}

.item-quantity .select-self-area {
  background: none;
  border: 1px solid #f0f0f0;
}

.item-quantity .select-self-area .select-ipt {
  display: inline-block;
  padding: 0 3px;
  width: 30px;
  min-width: 30px;
  text-align: center;
}
</style>
